package com.ruoyi.project.learn.ysdw.controller;

import java.util.List;

import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.framework.config.RuoYiConfig;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.learn.ysdw.domain.DjAudioBook;
import com.ruoyi.project.learn.ysdw.service.IDjAudioBookService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 有声读物Controller
 * 
 * @author ruoyi
 * @date 2025-09-17
 */
@Controller
@RequestMapping("/learn/ysdw")
public class DjAudioBookController extends BaseController
{
    private String prefix = "learn/ysdw";

    @Autowired
    private IDjAudioBookService djAudioBookService;

    @RequiresPermissions("learn:ysdw:view")
    @GetMapping()
    public String ysdw()
    {
        return prefix + "/ysdw";
    }

    /**
     * 查询有声读物列表
     */
    @RequiresPermissions("learn:ysdw:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DjAudioBook djAudioBook)
    {
        startPage();
        List<DjAudioBook> list = djAudioBookService.selectDjAudioBookList(djAudioBook);
        return getDataTable(list);
    }

    /**
     * 导出有声读物列表
     */
    @RequiresPermissions("learn:ysdw:export")
    @Log(title = "有声读物", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DjAudioBook djAudioBook)
    {
        List<DjAudioBook> list = djAudioBookService.selectDjAudioBookList(djAudioBook);
        ExcelUtil<DjAudioBook> util = new ExcelUtil<DjAudioBook>(DjAudioBook.class);
        return util.exportExcel(list, "有声读物数据");
    }

    /**
     * 新增有声读物
     */
    @RequiresPermissions("learn:ysdw:add")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存有声读物
     */
    @RequiresPermissions("learn:ysdw:add")
    @Log(title = "有声读物", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(DjAudioBook djAudioBook)
    {
        return toAjax(djAudioBookService.insertDjAudioBook(djAudioBook));
    }

    /**
     * 修改有声读物
     */
    @RequiresPermissions("learn:ysdw:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        DjAudioBook djAudioBook = djAudioBookService.selectDjAudioBookById(id);
        mmap.put("djAudioBook", djAudioBook);
        return prefix + "/edit";
    }

    /**
     * 修改保存有声读物
     */
    @RequiresPermissions("learn:ysdw:edit")
    @Log(title = "有声读物", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DjAudioBook djAudioBook)
    {
        return toAjax(djAudioBookService.updateDjAudioBook(djAudioBook));
    }

    /**
     * 删除有声读物
     */
    @RequiresPermissions("learn:ysdw:remove")
    @Log(title = "有声读物", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(djAudioBookService.deleteDjAudioBookByIds(ids));
    }
    /**
     * 上传MP3文件
     */
    @RequiresPermissions("learn:ysdw:upload")
    @Log(title = "有声读物", businessType = BusinessType.INSERT)
    @PostMapping("/upload")
    @ResponseBody
    public AjaxResult uploadFile(MultipartFile file)
    {
        try {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            String fileName = FileUploadUtils.upload(filePath, file, MimeTypeUtils.MEDIA_EXTENSION, true);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("fileName", fileName);
            ajax.put("message", "上传成功");
            ajax.put("url", fileName);
            return ajax;
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }


}
